import createSlice from '@reduxjs/toolkit'
import axios from 'axios'

const channelStore = createSlice({
  name: 'channelStore',
  initialState: {
    channels: []
  },
  reducers: {
    setChannels: (state, action) => {
      state.channels = action.payload
    }
  }
})

const {setChannels} = channelStore.actions;

const fetchChannelList = () => {
    return async (dispatch) => {
        const res = axios.get('https://geek.itheima.net/v1_0/channels')
        dispatch(setChannels((await res).data.data.channels))
    }
}
export {fetchChannelList,setChannels}

const channelReducer = channelStore.reducers;
export default channelReducer;